#!/bin/bash
RES="$(cd "$(dirname "$0")/../Resources" && pwd)"
LOG="$HOME/Library/Logs/YuvishanFreightsSecureBrowser.log"
mkdir -p "$(dirname "$LOG")"

PY=""
for cand in /opt/homebrew/bin/python3 /usr/local/bin/python3 /Library/Frameworks/Python.framework/Versions/Current/bin/python3 /usr/bin/python3; do
  if [ -x "$cand" ]; then
    PY="$cand"
    break
  fi
done

if [ -z "$PY" ]; then
  osascript -e 'display alert "Yuvishan Freights Secure Browser" message "Python 3 is required. Please install Python 3 from python.org or Homebrew (brew install python)." as critical'
  exit 1
fi

"$PY" "$RES/yf_browser_launcher.py" "$@" >>"$LOG" 2>&1
RC=$?
if [ $RC -ne 0 ]; then
  ERR="$(tail -n 5 "$LOG" | tr '"' "'")"
  osascript -e "display alert \"Yuvishan Freights Secure Browser\" message \"Launch failed: $ERR\" as critical"
fi
exit $RC
